package com.sambi.mvc.controller;

import com.sambi.mvc.FormControl;
import com.sambi.mvc.dao.InvoicesDao;
import com.sambi.mvc.model.Invoice;
import com.sambi.mvc.model.InvoicesSearchModel;
import com.sambi.mvc.view.Frame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author SAM BI
 */
public class InvoicesSearchController extends AbstractController {

    /**
     * properties constants called by the GUI event listeners of the view.
     */
    public static final String CUSTOMERID_PROPERTY = "CustomerId";
    public static final String CUSTOMERNAME_PROPERTY = "CustomerName";
    public static final String NBINVOICES_PROPERTY = "NbInvoices";

    private Frame frame;
    private List<InvoicesSearchModel> listModel;
    public static List<String> property;
    private Invoice invoice;

    public InvoicesSearchController(Frame frame) {
        this.frame = frame;
        listModel = new ArrayList<>();
        property = new ArrayList<>();
        initListener();
        initProperty();
        FormControl.init(null);
        invoice = new Invoice();
    }

    private void initProperty() {
        property.add(CUSTOMERID_PROPERTY);
        property.add(CUSTOMERNAME_PROPERTY);
        property.add(NBINVOICES_PROPERTY);
    }

    /**
     * methods called by the GUI event listeners of the view.
     */
    public void changeCustomerId(Integer newCustomerId) {
        setModelProperty(CUSTOMERID_PROPERTY, newCustomerId);
    }

    public void changeCustomerName(String newCustomerName) {
        setModelProperty(CUSTOMERNAME_PROPERTY, newCustomerName);
    }

    public void changeNbInvoices(String newNbInvoices) {
        invoice.setId(newNbInvoices);
//            invoice.setKeterangan("Belum Lunas");
        setModelProperty(NBINVOICES_PROPERTY, invoice);
//        if(newNbInvoices.equals("1")){
//            invoice.setId(newNbInvoices);
//            invoice.setKeterangan("Belum Lunas");
//            setModelProperty(NBINVOICES_PROPERTY, invoice);
//        }else{
//            invoice.setId(newNbInvoices);
//            invoice.setKeterangan("Lunas");
//            setModelProperty(NBINVOICES_PROPERTY, invoice);
//    }

}

//    public boolean validateParameters(String customerId, String nbInvoices) {
//        return this.validateCustomerId(customerId) && this.validateNbInvoices(nbInvoices);
//    }
public boolean validateCustomerId(String customerId) {
        //customerId: alphanumeric
        return Pattern.matches("[a-zA-Z0-9]+", customerId) && (customerId.length() <= 11);
    }

    public boolean validateNbInvoices(String nbInvoices) {
        // 0 <= nbInvoices <= 99 999
        return Pattern.matches("\\d+", nbInvoices) && (nbInvoices.length() <= 5);
    }

    private void initListener() {
        frame.getjTCustomerId().addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent event) {
                jTCustomerIdActionPerformed(event);
            }
        });
        frame.getjTCustomerId().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
        public void focusLost(FocusEvent event) {
                jTCustomerIdFocusLost(event);
            }

            @Override
        public void focusGained(FocusEvent event) {
                jTCustomerIdFocusGained(event);
            }
        });
        frame.getjTCustomerName().addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent event) {
                jTCustomerNameActionPerformed(event);
            }
        });
        frame.getjTCustomerName().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
        public void focusLost(FocusEvent event) {
                jTCustomerNameFocusLost(event);
            }
        });
        frame.getjTNbInvoices().addActionListener(new java.awt.event.ActionListener() {
            @Override
        public void actionPerformed(ActionEvent event) {
                jTNbInvoicesActionPerformed(event);
            }
        });
        frame.getjTNbInvoices().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
        public void focusLost(FocusEvent event) {
                jTNbInvoicesFocusLost(event);
            }

            @Override
        public void focusGained(FocusEvent event) {
                jTNbInvoicesFocusGained(event);
            }
        });
        frame.getjBSearch().addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent event) {
                jBSearchActionPerformed(event);
            }
        });
        frame.getjBCancel().addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent event) {
                jBCancelActionPerformed(event);
            }
        });
    }

    private void jTCustomerIdFocusLost(FocusEvent event) {
        frame.repaint(); // refresh the frame
        changeCustomerId(Integer.parseInt(frame.getjTCustomerId().getText()));
        System.out.println("Kundennummer: " + ((InvoicesSearchModel) getModel()).getCustomerId());
    }

    private void jTCustomerIdFocusGained(FocusEvent event) {
        if (validateCustomerId(frame.getjTCustomerId().getText())) {
            frame.getjTCustomerId().setForeground(new Color(0, 0, 0));
        }
    }

    private void jTCustomerIdActionPerformed(ActionEvent event) {
        changeCustomerId(Integer.parseInt(frame.getjTCustomerId().getText()));
    }

    //----Customer Name

    private void jTCustomerNameFocusLost(FocusEvent event) {
        System.out.println("Kunden Name: " + frame.getjTCustomerName().getText());
        frame.repaint(); // refresh the frame
        changeCustomerName(frame.getjTCustomerName().getText());
    }

    private void jTCustomerNameActionPerformed(ActionEvent event) {
        changeCustomerName(frame.getjTCustomerName().getText());
    }

    //----NbInvoices

    private void jTNbInvoicesFocusLost(FocusEvent event) {
        changeNbInvoices(frame.getjTNbInvoices().getText());
    }

    private void jTNbInvoicesFocusGained(FocusEvent event) {
        if (validateNbInvoices(frame.getjTNbInvoices().getText())) {
            frame.getjTNbInvoices().setForeground(new Color(0, 0, 0));
        }
    }

    private void jTNbInvoicesActionPerformed(ActionEvent event) {
        changeNbInvoices(frame.getjTNbInvoices().getText());
    }

    private void jBSearchActionPerformed(ActionEvent event) {
//        for(InvoicesSearchModel is:listModel)
        InvoicesDao.getInstance().save(getModel());
//        boolean valid = validateParameters(frame.getjTCustomerId().getText(), frame.getjTNbInvoices().getText());
//        System.out.println("valid: " + valid);
//        if (valid) {
//            frame.setVisible(false);
//            frame.dispose(); // destroy the JFrame object
//            System.exit(0);
//        } else {
//            if (!validateCustomerId(frame.getjTCustomerId().getText())) {
//                frame.getjTCustomerId().setForeground(new Color(255, 0, 0));
//                System.out.println("Kundennummer ist falsch");
//            }
//            if (!validateNbInvoices(frame.getjTNbInvoices().getText())) {
//                frame.getjTNbInvoices().setForeground(new Color(255, 0, 0));
//                System.out.println("Anzahl der Rechnungen ist falsch");
//            }
//        }
    }

    private void jBCancelActionPerformed(ActionEvent event) {
        InvoicesSearchModel is = (InvoicesSearchModel) getModel();
        listModel.add(is);
//        for(InvoicesSearchModel m:listModel)
//        System.out.println(m.getCustomerName());
    }
}
